#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "at32f4xx.h"
#include "at32_board.h"
#include "app.h"


TaskHandle_t button_Handler;
TaskHandle_t si24r1_Handler;
TaskHandle_t LED3_Handler;

void button_task_Function(void *pvParameters);


void LED3_task_Function(void *pvParameters);

RCC_ClockType RCC_Clocks;

int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	RCC_GetClocksFreq(&RCC_Clocks);
	usart1_init();
	button_pin_init();
	
  

	taskENTER_CRITICAL();
	if(xTaskCreate((TaskFunction_t )button_task_Function,     
				 (const char*    )"button_task",   
				 (uint16_t       )128, 
				 (void*          )NULL,
				 (UBaseType_t    )2,
				 (TaskHandle_t*  )&button_Handler) != pdPASS)
	{
		DBUG_SHOW("button task could not be created as there was insufficient heap memory remaining.\r\n");
	}        
	else
	{
		DBUG_SHOW("button task was created successfully.\r\n");
	}


	if(xTaskCreate((TaskFunction_t )si24r1_task_Function,     
				 (const char*    )"si24r1_task",   
				 (uint16_t       )128, 
				 (void*          )NULL,
				 (UBaseType_t    )2,
				 (TaskHandle_t*  )&si24r1_Handler) != pdPASS)
	{
		DBUG_SHOW("si24r1 task could not be created as there was insufficient heap memory remaining.\r\n");
	}        
	else
	{
		DBUG_SHOW("si24r1 task was created successfully.\r\n");
	}
	/* Create LED3 task */
	if(xTaskCreate((TaskFunction_t )LED3_task_Function,     
				 (const char*    )"LED3_task",   
				 (uint16_t       )128, 
				 (void*          )NULL,
				 (UBaseType_t    )2,
				 (TaskHandle_t*  )&LED3_Handler) != pdPASS) 
	{
	//printf("LED3 task could not be created as there was insufficient heap memory remaining.\r\n");
	}
	else
	{
	//printf("LED3 task was created successfully.\r\n");
	}

	/* Exit critical */            
	taskEXIT_CRITICAL();      
			  
	/* Start Scheduler */            
	vTaskStartScheduler(); 
}




/* LED3 task function */
void LED3_task_Function(void *pvParameters)
{
	while(1)
	{
		//DBUG_SHOW("LED3 task running\r\n");
		vTaskDelay(500);
	}
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}
#endif

/**
  * @}
  */

/**
  * @}
  */
